import '../../util/_base_export.dart';
import '../../util/_view_export.dart';

import '../../blocs/code.dart';
import '../../blocs/cart.dart';
import '../../util/app_util.dart';

class AccountVoucherUsagePage extends StatefulWidget {
  final String scenario;
  const AccountVoucherUsagePage(
    this.scenario, {
    Key? key,
  }) : super(key: key);
  @override
  _AccountVoucherUsagePageState createState() => _AccountVoucherUsagePageState();
}

class _AccountVoucherUsagePageState extends State<AccountVoucherUsagePage> {
  // _loadVoucher() async {
  //   AppSession.appUtil.loadVouchers({});
  // }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((callback) {
      // _loadVoucher();
    });
  }

  List<Widget> list = List<Widget>.empty(growable :true);

  @override
  Widget build(BuildContext context) {
    CartBloc _cart = Provider.of<CartBloc>(context, listen: false);

    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            child: Icon(
              Icons.chevron_left,
              color: AppColor.blackMain,
              size: 32,
            ),
          ),
        ),
        title: Text(
          AppText.text['account_voucher_usage'],
          style: TextStyle(color: AppColor.blackMain),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          color: Colors.grey[50],
          margin: EdgeInsets.only(top: 20.h),
          child: _voucherUsageList(context, _cart),
        ),
      ),
    );
  }

  Widget _voucherUsageList(BuildContext context, CartBloc ib_cart) {
    list.clear();
    for (var lv_voucher in AppSession.appUtil.vouchers) {
      list.add(GestureDetector(
        onTap: () async {},
        behavior: HitTestBehavior.opaque,
        child: _voucherUsageBlock(lv_voucher, ib_cart),
      ));
    }
    // if ((list ?? []).isEmpty)
    //   return Center(
    //     child: Column(
    //       children: <Widget>[
    //         Image.asset('assets/account/voucher_empty.png'),
    //         Text(AppText.text['account_voucher_novoucher'] ?? ''),
    //       ],
    //     ),
    //   );
    // else
    return Column(children: list);
  }

  Widget _voucherUsageBlock(Map iv_voucher, CartBloc ib_cart) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 18.w),
      child: Card(
        child: Container(
          height: 84.h,
          margin: EdgeInsets.symmetric(horizontal: 25.w),
          width: ScreenUtil().screenWidth.toDouble(),
          child: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  '退款',
                  style: AppTextStyle.nameVoucherUsage,
                ),
                Text(
                  '55.33',
                  style: AppTextStyle.amountVoucherUsage,
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  '#123123',
                  style: AppTextStyle.orderVoucherUsage,
                ),
                Text(
                  '2020.06.23',
                  style: AppTextStyle.dateVoucherUsage,
                ),
              ],
            ),
          ]),
        ),
      ),
    );
  }
}
